package com.bdc.whatwhat.receiver;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.PowerManager;
import android.widget.RemoteViews;

import com.bdc.whatwhat.activities.ChangeStatut;
import com.bdc.whatwhat.R;
import com.bdc.whatwhat.modele.Whatter;
import com.bdc.whatwhat.session.SessionManager;
import com.bdc.whatwhat.task.MonProfilTask;
import com.bdc.whatwhat.utils.Utility;
import com.bdc.whatwhat.widget.WidgetProvider;
import com.facebook.Session;

/**
 * Created by dev-w8-gfi on 25/06/2014.
 */
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver implements MonProfilTask.MonProfilTaskDelegate {

    public static long time = 1403704769682l;
    public static String statut = "";

    public static final String ROBOTO_LIGHT = "Roboto-Light.ttf";
    public static final String ROBOTO_BOLD = "Roboto-Bold.ttf";
    public static final String ROBOTO_BLACK = "Roboto-Black.ttf";
    public static final String ROBOTO_REGULAR = "Roboto-Regular.ttf";

    @Override
    public void onReceive(Context context, Intent intent) {

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");

       /* NE RIEN METTRE AU DESSUS DE SE PUTIN DE ACQUIRE MÊME PAS UN LOG
         SINON ETEINDRE TELEPHONE ET RALLUMER POUR UPDATE LE WIDGET
        Acquire the lock*/
        wl.acquire();

        //You can do the processing here update the widget/remote views.
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget_layout);

        ComponentName thiswidget = new ComponentName(context, WidgetProvider.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);


        if (intent.getAction() != null) {
            time = System.currentTimeMillis();
            Intent intentActivity = new Intent(context, ChangeStatut.class);
            intentActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //TODO: ajouter extra STATUS
            context.startActivity(intentActivity);
        }

        //TODO Verifier si l'utilisateur est connecter à l'apli
        SessionManager session = new SessionManager(context);

       // MonProfilTask task = new MonProfilTask(this);
        //task.execute(session.getToken());

        Whatter profil = session.getProfil();

        if(profil != null && profil.getWhatWhat() != null && profil.getWhatWhat().getTexte() != null) {
                statut = profil.getWhatWhat().getTexte();
        }

        if (session.getTime() == 0l) {
            time = System.currentTimeMillis();
            session.setTime(time);
        } else {
            time = session.getTime();
        }

        // On change le texte
        remoteViews.setImageViewBitmap(R.id.quoiquoi, buildUpdate(context, context.getString(R.string.tu_fais_quoi_quoi), ROBOTO_LIGHT, Color.WHITE, 500, 50, 25, 130, 20));
        remoteViews.setImageViewBitmap(R.id.statut, buildUpdate(context, statut, ROBOTO_REGULAR, Color.WHITE, 500, 100, 40, 210, 30));
        remoteViews.setImageViewBitmap(R.id.depuisValue, buildUpdate(context, Utility.getCurrentTime(System.currentTimeMillis()), ROBOTO_BLACK, Color.WHITE, 180, 50, 22, 50, 20));
        remoteViews.setImageViewBitmap(R.id.depuisText, buildUpdate(context, context.getString(R.string.depuis), ROBOTO_LIGHT, Color.WHITE, 135, 50, 22, 100, 20));

        // Register an onClickListener
        Intent clickIntent = new Intent(context,
                AlarmManagerBroadcastReceiver.class);

        clickIntent.setAction("Teub Update");
        clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
                manager.getAppWidgetIds(thiswidget));
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, clickIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        remoteViews.setOnClickPendingIntent(R.id.widget_relative, pendingIntent);


        manager.updateAppWidget(thiswidget, remoteViews);
        //Release the lock
        wl.release();
    }


    public Bitmap buildUpdate(Context context, String time, String font, int color, int width, int height, int textSize, float x, float y) {
        Bitmap myBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
        Canvas myCanvas = new Canvas(myBitmap);
        Paint paint = new Paint();
        Typeface clock = Typeface.createFromAsset(context.getAssets(), "fonts/" + font);
        paint.setAntiAlias(true);
        paint.setSubpixelText(true);
        paint.setTypeface(clock);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(color);
        paint.setTextSize(textSize);
        paint.setTextAlign(Paint.Align.CENTER);
        myCanvas.drawText(time, x, y, paint);
        return myBitmap;
    }

    @Override
    public void afficherChargement(boolean afficher) {

    }

    @Override
    public void onMonProfilRecu(Whatter monProfil) {

    }

    @Override
    public void onErreurReseau() {

    }
}